package com.tom.demo.service.common.impl;

import com.tom.demo.component.MinioComponent;
import com.tom.demo.service.common.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


@Slf4j
@Service
public class FileServiceImpl implements FileService {

    @Value("${bucketName}")
    private String bucketName;


    @Resource
    private MinioComponent minioComponent;


    @Override
    public String upload(MultipartFile file) {
        try {
            minioComponent.createBucket(bucketName);
            return minioComponent.uploadFile(bucketName, file);
        } catch (Exception e) {
            log.error("<<======>>上传文件失败,{}",e.getMessage());
        }
        return null;
    }


    @Override
    public void download(String fileUrl) {
//        InputStream object = minioUtils.getObject(filePath);
//        // 加载文件资源
//        org.springframework.core.io.Resource resource = new InputStreamResource(object);
//        // 设置下载文件的响应头
//        HttpHeaders headers = new HttpHeaders();
//        String fileName = filePath.substring(4);
//        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
//        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//        return ResponseEntity.ok().headers(headers).body(resource);
    }


}
